package com.example.cinema.controller;

import com.example.cinema.dto.ScheduleDTO;
import com.example.cinema.model.Schedule;
import com.example.cinema.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/schedules")
public class ScheduleController {

    private final ScheduleService scheduleService;

    @Autowired
    public ScheduleController(ScheduleService scheduleService) {
        this.scheduleService = scheduleService;
    }

    // 添加排片
    @PostMapping
    public ResponseEntity<Schedule> create(@RequestBody ScheduleDTO dto) {
        Schedule schedule = scheduleService.create(dto);
        return new ResponseEntity<>(schedule, HttpStatus.CREATED);
    }

    // 获取所有排片
    @GetMapping
    public ResponseEntity<List<ScheduleDTO>> getAllSchedules() {
        List<ScheduleDTO> schedules = scheduleService.getAllSchedules();
        return new ResponseEntity<>(schedules, HttpStatus.OK);
    }

    // 根据ID获取排片
    @GetMapping("/{id}")
    public ResponseEntity<ScheduleDTO> getScheduleById(@PathVariable Integer id) {
        ScheduleDTO schedule = scheduleService.getScheduleById(id);
        return new ResponseEntity<>(schedule, HttpStatus.OK);
    }

    // 获取排片详情(包含座位信息)
    @GetMapping("/{id}/details")
    public ResponseEntity<ScheduleService.ScheduleWithSeatsDTO> getScheduleWithSeats(@PathVariable Integer id) {
        ScheduleService.ScheduleWithSeatsDTO scheduleDetails = scheduleService.getScheduleWithSeats(id);
        return new ResponseEntity<>(scheduleDetails, HttpStatus.OK);
    }
}